﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

namespace SCSInternetBanking.NganLuong
{
    public class NL_Checkout
    {
        public NL_Checkout()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private String nganluong_url = "https://www.nganluong.vn/checkout.php";

        private String merchant_site_code = "23715";	//thay mã merchant site mà b?n dã dang ký vào dây

        private String secure_pass = "22051989";	//thay mật khẩu giao tiếp giữa website c?a b?n v?i NgânLu?ng.vn mà bạn đã ký vào dây


        public string GetMD5Hash(string input)
        {

            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);

            bs = x.ComputeHash(bs);

            System.Text.StringBuilder s = new System.Text.StringBuilder();

            foreach (byte b in bs)
            {

                s.Append(b.ToString("x2").ToLower());

            }

            string md5String = s.ToString();

            return md5String;
        }

        public string buildCheckoutUrlNew(string return_url, string receiver, string transaction_info, string order_code, string price, string currency = "vnd", string quantity = "0", string tax = "0", string discount = "0", string fee_cal = "0", string fee_shipping = "0", string order_description = "", string buyer_info = "", string affiliate_code = "")
        {
            // Tao bien secure code
            string secure_code = "";

            secure_code += this.merchant_site_code;
            secure_code += " " + return_url.ToLower();
            secure_code += " " + receiver;
            secure_code += " " + transaction_info;
            secure_code += " " + order_code;
            secure_code += " " + price;
            secure_code += " " + currency;
            secure_code += " " + quantity;
            secure_code += " " + tax;
            secure_code += " " + discount;
            secure_code += " " + fee_cal;
            secure_code += " " + fee_shipping;
            secure_code += " " + order_description;
            secure_code += " " + buyer_info;
            secure_code += " " + affiliate_code;
            secure_code += " " + this.secure_pass;

            // Tao mang bam
            Hashtable ht = new Hashtable();

            ht.Add("merchant_site_code", this.merchant_site_code);
            ht.Add("return_url", return_url.ToLower());
            ht.Add("receiver", receiver);
            ht.Add("transaction_info", transaction_info);
            ht.Add("order_code", order_code);
            ht.Add("price", price);
            ht.Add("currency", currency);
            ht.Add("quantity", quantity);
            ht.Add("tax", tax);
            ht.Add("discount", discount);
            ht.Add("fee_cal", fee_cal);
            ht.Add("fee_shipping", fee_shipping);
            ht.Add("order_description", order_description);
            ht.Add("buyer_info", buyer_info);
            ht.Add("affiliate_code", affiliate_code);
            ht.Add("secure_code", this.GetMD5Hash(secure_code));

            // Tao url redirect
            string redirect_url = this.nganluong_url;

            if (redirect_url.IndexOf("?") == -1)
            {
                redirect_url += "?";
            }
            else if (redirect_url.Substring(redirect_url.Length - 1, 1) != "?" && redirect_url.IndexOf("&") == -1)
            {
                redirect_url += "&";
            }

            string url = "";

            // Duyet cac phan tu trong mong bam ht1 de tao redirect url
            IDictionaryEnumerator en = ht.GetEnumerator();

            while (en.MoveNext())
            {
                if (url == "")
                    url += en.Key.ToString() + "=" + HttpUtility.UrlEncode(en.Value.ToString());
                else
                    url += "&" + en.Key.ToString() + "=" + HttpUtility.UrlEncode(en.Value.ToString());
            }

            string rdu = redirect_url + url;

            return rdu;
        }

        public Boolean verifyPaymentUrl(string transaction_info, string order_code, string price, string payment_id, string payment_type, string error_text, string secure_code)
        {
            // Tao ma xac thuc
            string str = "";

            str += " " + transaction_info;

            str += " " + order_code;

            str += " " + price;

            str += " " + payment_id;

            str += " " + payment_type;

            str += " " + error_text;

            str += " " + this.merchant_site_code;

            str += " " + this.secure_pass;

            // Ma hoa cac tham so
            string verify_secure_code = "";

            verify_secure_code = this.GetMD5Hash(str);

            // Xac thuc ma
            if (verify_secure_code == secure_code) return true;

            return false;
        }

        public String buildCheckoutUrl(String return_url, String receiver, String transaction_info, String order_code, String price)
        {
            // Tao bien secure code
            String secure_code = "";

            secure_code += this.merchant_site_code;

            secure_code += " " + HttpUtility.UrlEncode(return_url).ToLower();

            secure_code += " " + receiver;

            secure_code += " " + transaction_info;

            secure_code += " " + order_code;

            secure_code += " " + price;

            secure_code += " " + this.secure_pass;

            // Tao mang bam
            Hashtable ht = new Hashtable();

            ht.Add("merchant_site_code", this.merchant_site_code);

            ht.Add("return_url", HttpUtility.UrlEncode(return_url).ToLower());

            ht.Add("receiver", receiver);

            ht.Add("transaction_info", transaction_info);

            ht.Add("order_code", order_code);

            ht.Add("price", price);

            ht.Add("secure_code", this.GetMD5Hash(secure_code));

            // Tao url redirect
            String redirect_url = this.nganluong_url;

            if (redirect_url.IndexOf("?") == -1)
            {
                redirect_url += "?";
            }
            else if (redirect_url.Substring(redirect_url.Length - 1, 1) != "?" && redirect_url.IndexOf("&") == -1)
            {
                redirect_url += "&";
            }

            String url = "";

            // Duyet cac phan tu trong mong bam ht1 de tao redirect url
            IDictionaryEnumerator en = ht.GetEnumerator();

            while (en.MoveNext())
            {
                if (url == "")
                    url += en.Key.ToString() + "=" + en.Value.ToString();
                else
                    url += "&" + en.Key.ToString() + "=" + en.Value.ToString();
            }

            String rdu = redirect_url + url;

            return rdu;
        }
    
    }
}
